package com.moli.iov.admin.web.controller;

import com.moli.iov.admin.model.vo.req.CommandVo;
import com.moli.iov.admin.service.ICommandFacadeService;
import com.moli.iov.down.enums.OperateTypeEnum;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 命令
 * </p>
 *
 * @author zql
 * @since 2019/9/18 13:07
 */
@Slf4j
@RestController
@RequestMapping("/command")
public class CommandController extends BaseController {

  @Autowired
  private ICommandFacadeService commandService;

  /**
   * 控车
   * @param commandVo
   * @return
   */
  @RequestMapping("/control")
  public RestResponse control(@RequestBody @Validated({CommandVo.General.class, CommandVo.Control.class}) CommandVo commandVo){
    assembleUserInfo(commandVo);
    return commandService.sendCommand(commandVo);
  }

  /**
   * 车门控制
   * @param commandVo
   * @return
   */
  @RequestMapping("/door")
  public RestResponse door(@RequestBody @Validated({CommandVo.General.class, CommandVo.Door.class}) CommandVo commandVo){
    assembleUserInfo(commandVo);
    return commandService.sendCommand(commandVo);
  }

  /**
   * 车窗控制
   * @param commandVo
   * @return
   */
  @RequestMapping("/window")
  public RestResponse window(@RequestBody @Validated({CommandVo.General.class, CommandVo.Window.class}) CommandVo commandVo){
    assembleUserInfo(commandVo);
    return commandService.sendCommand(commandVo);
  }

  /**
   * 寻车
   * @param commandVo
   * @return
   */
  @RequestMapping("/searchCar")
  public RestResponse searchCar(@RequestBody @Validated({CommandVo.General.class}) CommandVo commandVo){
    assembleUserInfo(commandVo);
    return commandService.sendCommand(commandVo);
  }

  /**
   * 重启
   * @param commandVo
   * @return
   */
  @RequestMapping("/restart")
  public RestResponse restart(@RequestBody @Validated({CommandVo.General.class}) CommandVo commandVo){
    assembleUserInfo(commandVo);
    return commandService.sendCommand(commandVo);
  }

  /**
   * 设置上传频率
   * @param commandVo
   * @return
   */
  @RequestMapping("/frequency")
  public RestResponse frequency(@RequestBody @Validated({CommandVo.General.class, CommandVo.Frequency.class})
      CommandVo commandVo){
    assembleUserInfo(commandVo);
    return commandService.sendCommand(commandVo);
  }

  /**
   * 设置上传地址
   * @param commandVo
   * @return
   */
  @RequestMapping("/ipAndPort")
  public RestResponse ipAndPort(@RequestBody @Validated({CommandVo.General.class, CommandVo.IpAndPort.class})
      CommandVo commandVo){
    assembleUserInfo(commandVo);
    return commandService.sendCommand(commandVo);
  }

  /**
   * 恢复出厂
   * @param commandVo
   * @return
   */
  @RequestMapping("/init")
  public RestResponse init(@RequestBody @Validated({CommandVo.General.class}) CommandVo commandVo){
    assembleUserInfo(commandVo);
    return commandService.sendCommand(commandVo);
  }

  /**
   * 升级
   * @param commandVo
   * @return
   */
  @RequestMapping("/upgrade")
  public RestResponse upgrade(@RequestBody @Validated({CommandVo.General.class, CommandVo.Upgrade.class}) CommandVo commandVo){
    assembleUserInfo(commandVo);
    return commandService.sendUpgrade(commandVo);
  }

  /**
   * 批量升级
   * @param commandVo
   * @return
   */
  @RequestMapping("/batchUpgrade")
  public RestResponse batchUpgrade(@RequestBody @Validated({CommandVo.Upgrade.class}) CommandVo commandVo){
    assembleUserInfo(commandVo);
    if(commandVo.getVins()== null || commandVo.getVins().isEmpty()){
      return RestResponse.failure("vins不能为空");
    }
    return commandService.sendUpgrade(commandVo);
  }

  /**
   * 控车组
   * @param commandVo
   * @return
   */
  @RequestMapping("/controlGroup")
  public RestResponse controlGroup(@RequestBody @Validated({CommandVo.General.class}) CommandVo commandVo){
    if(!commandVo.getOperateTypes().contains(OperateTypeEnum.SEARCH_CAR.getFlag()) &&
        StringUtil.isBlank(commandVo.getMemo())){
      return RestResponse.failure("原因说明不可为空");
    }
    assembleUserInfo(commandVo);
    return commandService.sendCommand(commandVo);
  }
}
